package org.abratuhi.kvngroupware.dalgen;

import java.util.Properties;

import org.abratuhi.kvngroupware.network.AbstractDbMessageProceeder;
import org.jdom.Element;
import org.xmldb.api.base.ResourceSet;

public class DalgenDbMessageProceeder extends AbstractDbMessageProceeder{

	public DalgenDbMessageProceeder(Properties properties) {
		super(properties);
	}
	
	public Element[] getWords(int nWords){
		String query = "xquery version \"1.0\";" + 
						"declare namespace kvn=\"http://www.stone-sutras.org/xquery\";"+
						"declare function kvn:gen($n as xs:integer, $lb as xs:integer, $ub as xs:integer) as element()*{"+
						"let $r:=random($lb, $ub)"+
						"let $text:=//xdxf/ar[position()=$r]/k/text()"+
						"return if($n>0)"+
							"then (<word>{$text}</word>, kvn:gen($n - 1, $lb, $ub))"+
							"else <word>{$text}</word>"+
						"};"+
						"let $c:=count(//xdxf/ar/k)"+
						"return kvn:gen("+nWords+", 0, $c)";
		ResourceSet words = executeQuery(query);
		return convertResourceSetToElements(words);
	}

}
